<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * 微信二维码分组管理
 * @package App\Http\Controllers\Admin
 */
class WxQrcodeGroupController extends BaseController
{
    private $groupSer;
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->groupSer = new \App\Services\WxQrcodeGroupService();
    }

    /**
     * 分组列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxQrcodeGroup::select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('group_name', 'like', '%'.$keyword.'%');
            }
            return $query->paginate($pageSize);
        }
        return view('admin.wxqrcodegroup.index', $this->viewData);
    }
    /**
     * 新建分组
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxQrcodeGroupPost $request)
    {
        if ($request->ajax()){
            if($this->groupSer->save($request)){
                return $this->showMsg('success', '新增成功', [], 'admin.sysmenugroup.index');
            }else{
                return $this->showMsg('error');
            }
        }
    }
    /**
     * 删除
     * @param Request $request
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()){
            if($this->groupSer->del($request->input('group_id', 0))){
                return $this->showMsg('success', '删除成功', [], 'admin.sysmenugroup.index');
            }else{
                return $this->showMsg('error', '删除失败');
            }
        }
    }
}
